package com.pf.java;


import java.util.Comparator;
import java.util.TreeMap;

/**
 * @author pf
 * @data 2021/11/1-17:07
 */
public class treemap {

    public static void main(String[] args) {
        TreeMap map = null;

        //自然排序
        map = new TreeMap();
        map.put("Lily",12);
        map.put("Lucy",13);
        map.put("Lisa",14);
        map.put("Tony",15);
        map.put("Mary",16);
        System.out.println(map);

        System.out.println(map.firstKey()+ " , " + map.lastKey());
        System.out.println(map.lowerKey("Lucy") + " , " + map.higherKey("Lucy"));
        System.out.println(map.headMap("Lucy"));
        System.out.println(map.tailMap("Lucy"));
        System.out.println(map.subMap("Lisa" , "Tony"));


        // 定制排序
        map = new TreeMap(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                String s1 = (String) o1;
                String s2 = (String) o2;
                if (s1 == null && s2 == null) {
                    return 0;
                } else if (s1==null) {
                    return 1;
                } else if (s2 == null) {
                    return -1;
                } else {
//                    return s1.compareTo(s2) * (-1);
                    return s2.compareTo(s1);
                }
            }
        });

        map.put("Lily",12);
        map.put("Lucy",13);
        map.put("Lisa",14);
        map.put("Tony",15);
        map.put("Mary",16);
        System.out.println(map);


    }
}
